﻿/* This Component Library is licensed under the GNU General Public License version 2 (GPLv2) 
 * You can use this software and its associated files as you wish so long as derived versions
 * or works based on this library are also freely released.
 * If you require a commercial licence for this software without the above restrictions please
 * contact me via scott_blood@hotmail.com or via CodePlex.
 * (c) 2010 Scott Blood
 * Visit SGBControls.CodePlex.com for the latest version of this library.
 */ 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SGBControls.Editors
{
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(ListBox))]
    [DefaultBindingPropertyAttribute("SelectedValue")]
    public partial class SGBListBox : SGBBaseControl
    {
        #region event delegates
        public new event EventHandler Click;
        public new event EventHandler DoubleClick;
        public event DrawItemEventHandler DrawItem;
        public new event KeyEventHandler KeyDown;
        public new event KeyEventHandler KeyUp;
        public new event KeyPressEventHandler KeyPress;
        public new event MouseEventHandler MouseDown;
        public new event MouseEventHandler MouseUp;
        public new event MouseEventHandler MouseMove;
        public new event EventHandler MouseHover;
        public event ListControlConvertEventHandler Format;
        public event EventHandler FormatInfoChanged;
        public event EventHandler FormatStringChanged;
        public event EventHandler FormattingEnabledChanged;
        public event MeasureItemEventHandler MeasureItem;
        public new event MouseEventHandler MouseClick;
        public new event EventHandler MouseEnter;
        public new event EventHandler MouseLeave;
        public new event MouseEventHandler MouseWheel;
        public event EventHandler SelectedIndexChanged;
        public event EventHandler SelectedValueChanged;
        public event EventHandler ValueMemberChanged;
        #endregion

        #region private variables
        private Color fSelectedItemForeColor = SystemColors.HighlightText;
        private Color fSelectedItemBackColor = SystemColors.Highlight;        
        #endregion

        #region Standard ListBox Properties

        [Description("Gets or sets the width of columns in a multicolumn ListBox.")]
        public int ColumnWidth
        {
            get
            {
                return listBox1.ColumnWidth;
            }
            set
            {
                listBox1.ColumnWidth = value;
            }
        }

        [Description("Gets or sets the drawing mode for the control.")]
        public DrawMode DrawMode
        {
            get
            {
                return listBox1.DrawMode;
            }
            set
            {
                listBox1.DrawMode = value;
            }
        }

        [Description("Gets or sets a value indicating whether formatting is applied to the DisplayMember property of the ListControl.")]
        public string FormatString
        {
            get
            {
                return listBox1.FormatString;
            }
            set
            {
                listBox1.FormatString = value;
            }
        }

        [Description("Gets or sets a value indicating whether formatting is applied to the DisplayMember property of the ListControl. ")]
        public bool FormattingEnabled
        {
            get
            {
                return listBox1.FormattingEnabled;
            }
            set
            {
                listBox1.FormattingEnabled = value;
            }
        }

        [Description("Gets or sets the width by which the horizontal scroll bar of a ListBox can scroll.")]
        public int HorizontalExtent
        {
            get
            {
                return listBox1.HorizontalExtent;
            }
            set
            {
                listBox1.HorizontalExtent = value;
            }
        }

        [Description("Gets or sets a value indicating whether a horizontal scroll bar is displayed in the control.")]
        public bool HorizontalScrollbar
        {
            get
            {
                return listBox1.HorizontalScrollbar;
            }
            set
            {
                listBox1.HorizontalScrollbar = value;
            }
        }

        [Description("Gets or sets a value indicating whether the control should resize to avoid showing partial items.")]
        public bool IntegralHeight
        {
            get
            {
                return listBox1.IntegralHeight;
            }
            set
            {                
                listBox1.IntegralHeight = value;
            }
        }

        [Description("Gets or sets the height of an item in the ListBox.")]
        public int ItemHeight
        {
            get
            {
                return listBox1.ItemHeight;
            }
            set
            {
                listBox1.ItemHeight = value;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Gets or sets the zero-based index of the currently selected item in a ListBox.")]
        public int SelectedIndex
        {
            get
            {
                return listBox1.SelectedIndex;
            }
            set
            {
                listBox1.SelectedIndex = value;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Gets or sets the currently selected item in the ListBox.")]
        public object SelectedItem
        {
            get
            {
                return listBox1.SelectedItem;
            }
            set
            {
                listBox1.SelectedItem = value;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.")]
        public System.Windows.Forms.ListBox.SelectedIndexCollection SelectedIndices
        {
            get
            {
                return listBox1.SelectedIndices;
            }           
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Gets or sets the value of the member property specified by the ValueMember property.")]
        public object SelectedValue
        {
            get
            {                
                return listBox1.SelectedValue;
            }
            set
            {
                listBox1.SelectedValue = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.StringCollectionEditor", "System.Drawing.Design.UITypeEditor, System.Drawing")]
        [Description("Gets or sets the items displayed within the ListBox.")]
        public System.Windows.Forms.ListBox.ObjectCollection Items
        {
            get
            {
                return listBox1.Items;
            }
            set
            {
                listBox1.Items.Clear();
                listBox1.Items.AddRange(value);
            }
        }

        [Description("Gets or sets a value indicating whether the ListBox supports multiple columns.")]
        public bool MultiColum
        {
            get
            {
                return listBox1.MultiColumn;
            }
            set
            {
                listBox1.MultiColumn = value;
            }
        }

        [Description("Gets or sets a value indicating whether the vertical scroll bar is shown at all times.")]
        public bool ScrollAlwaysVisible
        {
            get
            {
                return listBox1.ScrollAlwaysVisible;
            }
            set
            {
                listBox1.ScrollAlwaysVisible = value;
            }
        }

        [Description("Gets or sets the method in which items are selected in the ListBox.")]
        public SelectionMode SelectionMode
        {
            get
            {
                return listBox1.SelectionMode;
            }
            set
            {
                listBox1.SelectionMode = value;                
            }
        }

        [Description("Gets or sets the property to use as the actual value for the items in the ListControl.")]
        public string ValueMember
        {
            get
            {
                return listBox1.ValueMember;
            }
            set
            {
                listBox1.ValueMember = value;
            }
        }

        [Description("Gets or sets a value indicating whether the items in the ListBox are sorted alphabetically.")]
        public bool Sorted
        {
            get
            {
                return listBox1.Sorted;
            }
            set
            {
                listBox1.Sorted = value;
            }
        }

        [Description("Gets or sets the current text in the ListBox.")]
        public override string Text
        {
            get
            {
                return this.listBox1.Text;
            }
            set
            {
                this.listBox1.Text = value;
            }
        }

        [Description("Gets or sets the foreground color of the control.")]
        public override Color ForeColor
        {
            get
            {
                return this.listBox1.ForeColor;
            }
            set
            {
                this.listBox1.ForeColor = value;
            }
        }

        [Description("Gets or sets the shortcut menu associated with the control.")]
        public override ContextMenu ContextMenu
        {
            get
            {
                return this.listBox1.ContextMenu;
            }
            set
            {
                this.listBox1.ContextMenu = value;
            }
        }

        #endregion

        #region New properties
        [Description("Gets or sets the Forecolor used for selected items within the Edit Control")]
        public Color SelectedItemForeColor
        {
            get
            {                        
                
                return fSelectedItemForeColor;
            }
            set
            {                
                fSelectedItemForeColor = value;
            }
        }        

        [Description("Gets or sets the background color used for selected items is selected within the Edit Control")]
        public Color SelectedItemBackColor
        {
            get
            {
                return fSelectedItemBackColor;
            }
            set
            {
                fSelectedItemBackColor = value;
            }
        }                
        #endregion

        #region New Methods
        [Description("Gets the native .net ListBox used to render this control.")]
        public override object GetNativeControl()
        {
            return listBox1;
        }
        #endregion

        #region Standard ListBox Events
        public void HookupEvents()
        {        
            listBox1.Click += new EventHandler(listBox1_Click);
            listBox1.DoubleClick += new EventHandler(listBox1_DoubleClick);
            listBox1.KeyDown += new KeyEventHandler(listBox1_KeyDown);
            listBox1.KeyUp += new KeyEventHandler(listBox1_KeyUp);
            listBox1.KeyPress += new KeyPressEventHandler(listBox1_KeyPress);
            listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
            listBox1.MouseUp += new MouseEventHandler(listBox1_MouseUp);
            listBox1.MouseMove += new MouseEventHandler(listBox1_MouseMove);
            listBox1.MouseHover += new EventHandler(listBox1_MouseHover);
            listBox1.Format += new ListControlConvertEventHandler(listBox1_Format);
            listBox1.FormatInfoChanged += new EventHandler(listBox1_FormatInfoChanged);
            listBox1.FormatStringChanged += new EventHandler(listBox1_FormatStringChanged);
            listBox1.FormattingEnabledChanged += new EventHandler(listBox1_FormattingEnabledChanged);
            listBox1.MeasureItem += new MeasureItemEventHandler(listBox1_MeasureItem);
            listBox1.MouseClick += new MouseEventHandler(listBox1_MouseClick);
            listBox1.MouseEnter += new EventHandler(listBox1_MouseEnter);
            listBox1.MouseLeave += new EventHandler(listBox1_MouseLeave);
            listBox1.MouseWheel += new MouseEventHandler(listBox1_MouseWheel);
            listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
            listBox1.SelectedValueChanged += new EventHandler(listBox1_SelectedValueChanged);
            listBox1.ValueMemberChanged += new EventHandler(listBox1_ValueMemberChanged);
        }

        void listBox1_ValueMemberChanged(object sender, EventArgs e)
        {
            if (ValueMemberChanged != null)
                ValueMemberChanged(sender, e);
        }

        void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (SelectedValueChanged != null)
                SelectedValueChanged(sender, e);
        }

        void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChanged != null)
                SelectedIndexChanged(sender, e);
        }

        void listBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (MouseWheel != null)
                MouseWheel(sender, e);
        }

        void listBox1_MouseLeave(object sender, EventArgs e)
        {
            if (MouseLeave != null)
                MouseLeave(sender, e);
        }

        void listBox1_MouseEnter(object sender, EventArgs e)
        {
            if (MouseEnter != null)
                MouseEnter(sender, e);
        }

        void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (MouseClick != null)
                MouseClick(sender, e);
        }

        void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            if (MeasureItem != null)
                MeasureItem(sender, e);
        }

        void listBox1_FormattingEnabledChanged(object sender, EventArgs e)
        {
            if (FormattingEnabledChanged != null)
                FormattingEnabledChanged(sender, e);
        }

        void listBox1_FormatStringChanged(object sender, EventArgs e)
        {
            if (FormatStringChanged != null)
                FormatStringChanged(sender, e);
        }

        void listBox1_FormatInfoChanged(object sender, EventArgs e)
        {
            if (FormatInfoChanged != null)
                FormatInfoChanged(sender, e);
        }

        void listBox1_Format(object sender, ListControlConvertEventArgs e)
        {
            if (Format != null)
                Format(sender, e);
        }

        void listBox1_MouseHover(object sender, EventArgs e)
        {            
            if (MouseHover != null)
                MouseHover(sender, e);
        }       

        void listBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseMove != null)
                MouseMove(sender, e);
        }

        void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (MouseUp != null)
                MouseUp(sender, e);
        }

        void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseDown != null)
                MouseDown(sender, e);
        }

        void listBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (KeyPress != null)
                KeyPress(sender, e);
        }

        void listBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (KeyUp != null)
                KeyUp(sender, e);
        }

        void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (KeyDown != null)
                KeyDown(sender, e);
        }

        void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (DoubleClick != null)
                DoubleClick(sender, e);
        }

        void listBox1_Click(object sender, EventArgs e)
        {
            if (Click != null)
                Click(sender, e);
        }
        #endregion

        #region constructor
        public SGBListBox()
        {
            InitializeComponent();
            HookupEvents();
        }
        #endregion

        #region overridden events

        private void listBox1_Enter(object sender, EventArgs e)
        {
            if (StyleController != null)
            {
                listBox1.BackColor = StyleController.ActiveBackgroundColor;
                listBox1.ForeColor = StyleController.ActiveForeColor;
            }
           // base.OnEnter(e);
        }

        private void listBox1_Leave(object sender, EventArgs e)
        {
            if (StyleController != null)
            {
                listBox1.BackColor = StyleController.InActiveBackgroundColor;
                listBox1.ForeColor = StyleController.InActiveForeColor;
            }
         //   base.OnLeave(e);
        }

        private void listBox1_SizeChanged(object sender, EventArgs e)
        {
            ListBox lb = sender as ListBox;
            Height = lb.Height + 8;
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (StyleController != null)
            {
                if ((e.State & DrawItemState.Selected) > 0)
                {
                    SolidBrush textBrush = new SolidBrush(StyleController.SelectedItemForeColor);
                    e.Graphics.FillRectangle(new SolidBrush(StyleController.SelectedItemBackColor), e.Bounds);
                    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds);
                    e.DrawFocusRectangle();
                    textBrush.Dispose();
                }
                else
                {
                    SolidBrush textBrush = new SolidBrush(ForeColor);
                    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds);
                    textBrush.Dispose();
                }
            }
            else
            {
                SGBEditStyleController Controller = new SGBEditStyleController();
                Controller.SelectedItemBackColor = fSelectedItemBackColor;
                Controller.SelectedItemForeColor = fSelectedItemForeColor;
                if ((e.State & DrawItemState.Selected) > 0)
                {
                    SolidBrush textBrush = new SolidBrush(Controller.SelectedItemForeColor);
                    e.Graphics.FillRectangle(new SolidBrush(Controller.SelectedItemBackColor), e.Bounds);
                    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds);
                    e.DrawFocusRectangle();
                    textBrush.Dispose();
                }
                else
                {
                    SolidBrush textBrush = new SolidBrush(ForeColor);
                    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds);
                    textBrush.Dispose();
                }
            }
            if (DrawItem != null)
                DrawItem(sender, e);
        }
        #endregion               
    }
}
